#!/usr/bin/env python3
# -*-coding:utf-8-*-

import docxtpl
import docx2pdf
from pydocx import PyDocX
from docx.shared import Mm
import sys
import os


def show_output(document, open_type='doc', output_doc='output_doc.docx', output_html='output_doc.html'):
    if sys.platform == 'win32':
        prefix = 'start '
    else:
        prefix = 'open '

    file_name = os.path.splitext(output_doc)[0]

    # --- 输出doc
    document.save(output_doc)

    # --------- 转化为html
    html = PyDocX.to_html(output_doc)
    f = open(output_html, 'w', encoding="utf-8")
    f.write(html)
    f.close()

    # --- convert to pdf
    # from bdtime import tt
    # tt.sleep(0.1)
    output_pdf = file_name + '.pdf'
    docx2pdf.convert(os.path.abspath(output_doc), os.path.abspath(output_pdf))
    os.system(prefix + output_pdf)

    if open_type == '__None__':
        print('--- 输出路径:', output_doc)
        return False
    elif open_type == '__all__':
        os.system(prefix + output_doc)
        os.system(prefix + output_html)
        os.system(prefix + output_pdf)
    elif open_type == 'doc':
        os.system(prefix + output_doc)
    elif open_type == 'html':
        os.system(prefix + output_html)
    elif open_type == 'pdf':
        os.system(prefix + output_pdf)
    else:
        raise ValueError('open_type取值错误!')
    return True


class ConvDocTemplateToPdf:
    """
    将doc模板转为pdf
    """
    def __init__(self, context, docx_path):
        """
        按照context将内容填入docx模板中

        context: 替换文本
        docx_path: docx模板路径
        """
        self.context = context
        self.daily_docx = docxtpl.DocxTemplate(docx_path)

    def render(self):
        self.daily_docx.render(self.context)

    def run(self):
        self.render()
        show_output(self.daily_docx, open_type='__all__')

    def get_outputs(self):
        """
        获得输出的文件
        """
        pass


def main():
    # 要编辑的docx文档模板路径
    docx_path = sys.path[0] + os.sep + r"input.docx"
    # 要插入的图片1路径
    image1_path = os.path.join(sys.path[0], 'temp', 'a1.jpg')
    # 要插入的图片2路径
    image2_path = os.path.join(sys.path[0], 'temp', 'b1.jpg')

    # 创建docx对象
    daily_docx = docxtpl.DocxTemplate(docx_path)
    # 创建2张图片对象
    insert_image1 = docxtpl.InlineImage(daily_docx, image1_path, width=Mm(30), height=Mm(50))
    insert_image2 = docxtpl.InlineImage(daily_docx, image2_path, width=Mm(50), height=Mm(30))

    # 渲染内容
    context = {
        # 'name': '博得',
        # 'phone': 18510779999,
        # 'sex': '男',
        # 'id_card': 43250311111111,
        # 'card_type': '大陆身份证',
        'work_unit': '新东方烹饪学校',
        'education_degree': '小学生',
        'school': '蓝翔',
        'month': 2,
        'year': 2021,
        "portraitPicture": insert_image1,
        "signaturePicture": insert_image2,
        'created_y': 2021,
        'created_m': 1,
        'created_d': 13,
    }

    # 渲染docx
    daily_docx.render(context)

    # # 保存docx
    # output_doc = 'new_docx.docx'
    # daily_docx.save(output_doc)
    #
    # import os
    # os.system('start ' + output_doc)

    from Doc import show_output
    show_output(daily_docx, open_type='__all__')


if __name__ == '__main__':
    main()
